package com.netease.xone.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.LinearLayout;

import com.netease.log.NTLog;
import com.netease.pkgRelated.XoneConsts;
import com.netease.xone.R;
import com.netease.xone.fragment.FragmentLogin;

public class ActivityLogin extends ActivityX1Base {
    private FragmentLogin mFragmentLogin;
	
	public static void launch(Context context,boolean switchToMain){
		Intent intent = new Intent(context, ActivityLogin.class);
		//非activity的context启动activity会出错
		if(!(context instanceof Activity))
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra(XoneConsts.BUNDLE_KEY.SWITCH_TO_MAIN, switchToMain);
		context.startActivity(intent);
	}
	public static void launch(Context context,boolean switchToMain,int errCode){
        Intent intent = new Intent(context, ActivityLogin.class);
        //非activity的context启动activity会出错
        if(!(context instanceof Activity))
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(XoneConsts.BUNDLE_KEY.SWITCH_TO_MAIN, switchToMain);
        intent.putExtra(XoneConsts.BUNDLE_KEY.SWITCH_TO_LOGIN_ERRCODE, errCode);
        context.startActivity(intent);
    }
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		LinearLayout linear = new LinearLayout(this);
		linear.setId(R.id.activity_login_container_id);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT);
		linear.setLayoutParams(lp);
		setContentView(linear);

		// NTLog.i("ActivityLogin", "savedInstanceState is " +
		// savedInstanceState);
		if (findViewById(R.id.activity_login_container_id) != null
				&& savedInstanceState == null) {
			FragmentManager fm = getSupportFragmentManager();
			FragmentTransaction ft = fm.beginTransaction();
			mFragmentLogin = new FragmentLogin();
			ft.add(R.id.activity_login_container_id, mFragmentLogin);
			ft.commit();
		}
	}
	@Override
	protected void onNewIntent(Intent intent) {
	    super.onNewIntent(intent);
	    NTLog.v("ActivityLogin", "onNewIntent ");
	}
	@Override
	public void onBackPressed() {
	    if(mFragmentLogin == null || !mFragmentLogin.onBack()) {
	        super.onBackPressed();
        } 
	}

}
